package cn.com.pureAppApi.serviceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import cn.com.pureAppApi.utils.PageUtils;
import cn.com.pureAppApi.utils.Query;

import cn.com.pureAppApi.entity.NewsEntity;
import cn.com.pureAppApi.service.INewsService;
import cn.com.pureAppApi.dao.NewsMapper;
import cn.com.pureAppApi.entity.vo.NewsEntityVo;


import com.github.pagehelper.PageHelper;
import cn.com.pureAppApi.utils.ResponseResult;


/**
 * 接口实现ServiceImpl
 * InnoDB free: 10240 kB
 *
 * QQ讨论群:76989293
 * @email 2696701142@qq.com
 * @date 2018-06-12 16:51:48
 */
@Service
public class NewsServiceImpl   implements INewsService {

	@Autowired
	private NewsMapper themapper;
	
	@Override
	public List<NewsEntityVo> findListPageVo(Integer page, Integer rows) {	
		NewsEntity record= new NewsEntity();
		PageHelper.startPage(page, rows, false);
		List<NewsEntity> list = themapper.select(record);		
		List<NewsEntityVo> listVo = new ArrayList<NewsEntityVo>();
		if(list!=null && list.size()>0){
			for (NewsEntity entity : list) {
			NewsEntityVo vo  =new NewsEntityVo();
					vo.setId(entity.getId());// id 
					vo.setUserId(entity.getUserId());// 用户名 
					vo.setTitle(entity.getTitle());// 标题 
					vo.setImg(entity.getImg());// 图片 
					vo.setContent(entity.getContent());// 内容 
					vo.setDatetime(entity.getDatetime());// 时间 
					listVo.add(vo);
			}
		}
		return listVo;
		//分页查询		

	}
	
	@Override
	public List<NewsEntityVo> findListPageVoByParamId(Integer page, Integer rows,Long oneByParamId) {	
		NewsEntity record= new NewsEntity();
		//record.setId(oneByParamId);
		PageHelper.startPage(page, rows, false);
		List<NewsEntity> list = themapper.select(record);		
		List<NewsEntityVo> listVo = new ArrayList<NewsEntityVo>();
		if(list!=null && list.size()>0){
			for (NewsEntity entity : list) {
				NewsEntityVo vo  =new NewsEntityVo();
							vo.setId(entity.getId());// id 
							vo.setUserId(entity.getUserId());// 用户名 
							vo.setTitle(entity.getTitle());// 标题 
							vo.setImg(entity.getImg());// 图片 
							vo.setContent(entity.getContent());// 内容 
							vo.setDatetime(entity.getDatetime());// 时间 
							listVo.add(vo);
			}
		}
		return listVo;
	}
	
	@Override
	public NewsEntityVo findSelectOneVoByParamId(Long oneByParamId) {	
			NewsEntity record= new NewsEntity();
			//record.setId(oneByParamId);		
			NewsEntity entity = themapper.selectOne(record);		
			NewsEntityVo vo  =new NewsEntityVo();
			if(entity!=null ){
					vo.setId(entity.getId());// id 
					vo.setUserId(entity.getUserId());// 用户名 
					vo.setTitle(entity.getTitle());// 标题 
					vo.setImg(entity.getImg());// 图片 
					vo.setContent(entity.getContent());// 内容 
					vo.setDatetime(entity.getDatetime());// 时间 
						}
			return vo;
	}
	
	@Override
   	public NewsEntityVo insertReturnVo(NewsEntityVo vo){
   		if (null==vo) {
			ResponseResult.error();
		}
		NewsEntity record= new NewsEntity();
		if(vo!=null){
		record.setId(vo.getId());// id 
		record.setUserId(vo.getUserId());// 用户名 
		record.setTitle(vo.getTitle());// 标题 
		record.setImg(vo.getImg());// 图片 
		record.setContent(vo.getContent());// 内容 
		record.setDatetime(vo.getDatetime());// 时间 
			}
		themapper.insertSelective(record);
		return vo;
   	}
   	
   	@Override
   	public NewsEntityVo editReturnVo(NewsEntityVo vo){
   		if (null==vo) {
			ResponseResult.error();
		}
		NewsEntity record= new NewsEntity();
		if(vo!=null){
			record.setId(vo.getId());// id 
			record.setUserId(vo.getUserId());// 用户名 
			record.setTitle(vo.getTitle());// 标题 
			record.setImg(vo.getImg());// 图片 
			record.setContent(vo.getContent());// 内容 
			record.setDatetime(vo.getDatetime());// 时间 
				}
		themapper.updateByPrimaryKeySelective(record);
		return vo;
   	}
   	
	@Override
	public List<NewsEntity> findListPage(Integer page, Integer rows) {
		//分页查询		
		NewsEntity record= new NewsEntity();
		PageHelper.startPage(page, rows, false);
		List<NewsEntity> list = themapper.selectAll();		
		return list;
	}
	
	@Override
	public List<NewsEntity>findListByParamId(Integer page, Integer rows,Long oneByParamId){
		//分页查询		
		NewsEntity record= new NewsEntity();
		//record.setId(oneByParamId)
		PageHelper.startPage(page, rows, false);
		List<NewsEntity> list = themapper.select(record);		
		return list;
	}
 
   	 
   	@Override
   	public NewsEntity insert(NewsEntity vo){
   		if (null==vo) {
			ResponseResult.error();
		}
		NewsEntity record= new NewsEntity();
		if(vo!=null){
		record.setId(vo.getId());// id 
		record.setUserId(vo.getUserId());// 用户名 
		record.setTitle(vo.getTitle());// 标题 
		record.setImg(vo.getImg());// 图片 
		record.setContent(vo.getContent());// 内容 
		record.setDatetime(vo.getDatetime());// 时间 
			}
		themapper.insertSelective(record);
		return vo;
   	}
   	
   	@Override
   	public NewsEntity edit(NewsEntity vo){
   		if (null==vo) {
			ResponseResult.error();
		}
		NewsEntity record= new NewsEntity();
		if(vo!=null){
		record.setId(vo.getId());// id 
		record.setUserId(vo.getUserId());// 用户名 
		record.setTitle(vo.getTitle());// 标题 
		record.setImg(vo.getImg());// 图片 
		record.setContent(vo.getContent());// 内容 
		record.setDatetime(vo.getDatetime());// 时间 
			}
		themapper.updateByPrimaryKeySelective(record);
		return vo;
   	}
   	
   	@Override
	public int save(NewsEntity vo){
		if (null==vo) {
			ResponseResult.error();
		}
		NewsEntity record= new NewsEntity();
		if(vo!=null){
		record.setId(vo.getId());// id 
		record.setUserId(vo.getUserId());// 用户名 
		record.setTitle(vo.getTitle());// 标题 
		record.setImg(vo.getImg());// 图片 
		record.setContent(vo.getContent());// 内容 
		record.setDatetime(vo.getDatetime());// 时间 
			}
		return themapper.insertSelective(record);
	}
	
	@Override
	public int update(NewsEntity vo){
		if (null==vo) {
			ResponseResult.error();
		}
		NewsEntity record= new NewsEntity();
		if(vo!=null){
		record.setId(vo.getId());// id 
		record.setUserId(vo.getUserId());// 用户名 
		record.setTitle(vo.getTitle());// 标题 
		record.setImg(vo.getImg());// 图片 
		record.setContent(vo.getContent());// 内容 
		record.setDatetime(vo.getDatetime());// 时间 
			}
		return themapper.updateByPrimaryKeySelective(record);
	}
	
	@Override
	public int remove(Integer id){
		return themapper.deleteByPrimaryKey(id);
	}
}
